package com.xl.rentkeeper.act;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import com.xl.fuse.protos.account.User;
import com.xl.rentkeeper.App;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.business.UserLogic;
import com.xl.rentkeeper.log.QLog;
import com.xl.rentkeeper.util.DialogUtil;

/**
 * Created by zwc on 2015/6/25.
 */
public class LoginAct extends BaseAct implements View.OnClickListener {
    private Dialog mDialog;
    private TextView mName;
    private TextView mPwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_login);
        setTranslucentStatus();
        if (UserLogic.getInstance().getUid() > 0) {
            startMain();
            finish();
            return;
        }
        mName = (TextView) findViewById(R.id.account);
        mPwd = (TextView) findViewById(R.id.pass);


        mPwd.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_GO || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    onLogin();
                    return true;
                }
                return false;
            }
        });

        findViewById(R.id.bt_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onLogin();
            }
        });

    }

    void onLogin() {
        String name = mName.getText().toString().trim();
        String pwd = mPwd.getText().toString().trim();
        if ("".equals(name)){
            App.showToast("请输入账号");
            return;
        }else  if ("".equals(pwd)){
            App.showToast("请输入密码");
            return;
        }

        closeKeyboard(mPwd);

        mDialog = DialogUtil.showProgressDialog(LoginAct.this,R.string.logining);
        User user = new User.Builder().name(name).pass(pwd).build();
        UserLogic.getInstance().login(user);
    }


    @Override
    public String[] monitorEvents() {
        return new String[]{CMD_USER_LOGIN};
    }

    @Override
    public void onEvent(String event, boolean ret,String errInfo, Object[] data) {
        if (event.equals(CMD_USER_LOGIN)) {
            if(mDialog!=null && mDialog.isShowing())mDialog.dismiss();
            QLog.d(this, "login result " + ret);
            if (ret) {
                startMain();
            }else{
                if(data!=null && data.length>0 && data[0]!=null){
                    int intRet = (Integer)data[0];
                    App.showToast(errInfo);
                }
            }
        }
    }
    void startMain() {
        Intent in = new Intent(this, MainActivity.class);
        in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(in);
        finish();
    }

    @Override
    public void onClick(View v) {

    }
}
